package logic

import (
	"context"
	"time"
	"xialou_platform/app/errorx"
	"xialou_platform/services/agent/model"

	"xialou_platform/services/agent/rpc/agentClient"
	"xialou_platform/services/agent/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type StoreLogAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewStoreLogAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StoreLogAddLogic {
	return &StoreLogAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *StoreLogAddLogic) StoreLogAdd(in *agentClient.StoreLog) (*agentClient.IdBody, error) {
	id := l.svcCtx.Flake.Generate().Int64()
	nowTime := time.Now()
	_, err := l.svcCtx.StoreLogModel.Insert(l.ctx, &model.StoreLog{
		Id:         id,
		StoreId:    in.StoreId,
		Status:     in.Status,
		Body:       in.Body,
		Operation:  in.Operation,
		CreateTime: nowTime,
		UpdateTime: nowTime,
		Remark:     in.Remark,
		AgentId:    in.AgentId,
		Reviewer:   in.Reviewer,
	})
	if err != nil {
		return nil, errorx.NewReasonError("网络出小差了，请重试刷新再试。", err.Error())
	}

	return &agentClient.IdBody{
		Id: id,
	}, nil
}
